package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Areas;
import com.thinkgem.jeesite.modules.sys.entity.TbCommunity;
import com.thinkgem.jeesite.modules.sys.entity.TbOrder;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.PC;
import com.thinkgem.jeesite.modules.sys.service.ShopAddService;
import com.thinkgem.jeesite.modules.sys.service.TbCommunityService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by lishuai on 2016/10/18 0017.
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/community")
public class TbCommunityController extends BaseController{

    @Autowired
    private TbCommunityService tbCommunityService;
    @Autowired
    private ShopAddService shopAddService;
    @Autowired
    PC service;// 调用业务层方法
    @ModelAttribute
    public TbCommunity get(@RequestParam(required=false) Integer id) {
        if (null!=id){
            return tbCommunityService.get(id);
        }else{
            return new TbCommunity();
        }
    }
    // 代理商查看他所在区域的小区列表
    @RequestMapping(value = "listCommunity")
    public String queryListCommunity(TbCommunity tbCommunity, HttpServletRequest request, HttpServletResponse response, Model model){
        User user = UserUtils.getUser();
        tbCommunity.setUid(user.getId());
        Page<TbCommunity> page = tbCommunityService.findByPage(new Page<TbCommunity>(request, response),tbCommunity);
        model.addAttribute("page", page);
        if(page.getList().size()==0){
            model.addAttribute("message1","无查询结果");
        }
        return "modules/sys/communityList";
    }
    //管理员查看所有小区
    @RequestMapping(value = "allCommunity")
    public String allCommunity(TbCommunity tbCommunity, HttpServletRequest request, HttpServletResponse response, Model model){
        Page<TbCommunity> page = tbCommunityService.allCommunity(new Page<TbCommunity>(request, response),tbCommunity);
        model.addAttribute("page", page);
        if(page.getList().size()==0){
            model.addAttribute("message1","无查询结果");
        }
        return "modules/sys/allCommunity";
    }
    @RequestMapping(value = "form")
    public String form(TbCommunity tbCommunity, Model model) {
        //得到代理商所在的市的id，查询得到区域列表
        User ares=shopAddService.userAreas(UserUtils.getUser().getId());
        User user=UserUtils.getUser();
        tbCommunity.setCityName(user.getCitys());
        tbCommunity.setProvinceName(user.getProvinces());
        //根据城市得到区域集合
        int cid=Integer.parseInt(ares.getCitys());
        List<Areas> area1 = service.showAreaListBycid(cid);
        model.addAttribute("areasList",area1);
        if(tbCommunity.getId()!=null){
            TbCommunity tt=tbCommunityService.ares(tbCommunity.getAreaId()+"");
            tbCommunity.setAreaId(tt.getAreaId());
        }
        model.addAttribute("tbCommunity",tbCommunity);
        //model.addAttribute("proList", service.showProvinceList());// 存入作用域中
        return "modules/sys/communityForm";
    }
    /**
     * 验证小区名称是否有效
     * @param oldCommName
     * @param commName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkCommName")
    public String checkCommName(String oldCommName, String commName) {
        if (commName !=null && commName.equals(oldCommName)) {
            return "true";
        } else if (commName !=null && tbCommunityService.getCommitByCommName(commName) == null) {
            return "true";
        }
        return "false";
    }
    /**
     * 验证小区名称是否有效
     * @param oldCommName
     * @param commName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "agentCheckCommName")
    public String agentCheckCommName(String oldCommName, String commName,Integer aes) {
        if (commName !=null && commName.equals(oldCommName)) {
            return "true";
        } else if (commName !=null && tbCommunityService.agentCheckCommName(commName,aes) == null) {
            return "true";
        }
        return "false";
    }

    // 保存
    @RequestMapping(value = "/communityEdit")
    public String communityEdit(TbCommunity tbCommunity,Model model,RedirectAttributes redirectAttributes){
        User currentUser = UserUtils.getUser();
        int result=0;
        if (StringUtils.isNotBlank(tbCommunity.getCommName())){
            result =  tbCommunityService.saveOrUpdateAdvertise(tbCommunity);
        }
        if(result>0){
            addMessage(redirectAttributes, "编辑小区成功");
        }
        return "redirect:listCommunity";
    }
    @RequestMapping(value = "delete")
    public String delete(TbCommunity tbCommunity, RedirectAttributes redirectAttributes) {
        if(Global.isDemoMode()){
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/sys/advertise/advertiseList/?repage";
        }
        int result = tbCommunityService.delete(tbCommunity);
        if(result>0){
            addMessage(redirectAttributes, "删除小区成功！");
        }else{
            addMessage(redirectAttributes, "删除小区失败，请重试！");
        }

        return "redirect:" + adminPath + "/sys/community/listCommunity/?repage";
    }

//    // 查看状态 隐藏
//    @ResponseBody
//    @RequestMapping("/isActive")
//    public String updateIsActive(@PathVariable("id") Integer id, boolean bool, Model model){
//        TbCommunity tbCommunity = tbCommunityService.selectByPrimaryKey(id);
//        tbCommunity.setIsActive(bool);
//        model.addAttribute("isActive", tbCommunity.getIsActive());
//        Integer line = tbCommunityService.updateByPrimaryKeySelective(tbCommunity);
//        String result = line.toString();
//        return result;
//    }

}
